//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbLuongKhoan
    {
        public static DataTable Search(ETbLuongKhoan lk)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenLK", lk.TenLK);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLuongKhoan_Search", pr);
            return all;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbLuongKhoan lk)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLK", lk.TenLK);
            pr[1] = new SqlParameter(@"MaLK", lk.MaLK);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLuongKhoan_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbLuongKhoan lk)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenLK", lk.TenLK);
          
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLuongKhoan_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLuongKhoan_SelectAll", null);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbLuongKhoan OTbLuongKhoan)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLK", OTbLuongKhoan.TenLK);
            pr[1] = new SqlParameter(@"MoTa", OTbLuongKhoan.MoTa);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLuongKhoan_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbLuongKhoan OTbLuongKhoan)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaLK", OTbLuongKhoan.MaLK);
            pr[1] = new SqlParameter(@"TenLK", OTbLuongKhoan.TenLK);
            pr[2] = new SqlParameter(@"MoTa", OTbLuongKhoan.MoTa);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLuongKhoan_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaLK)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLK",MaLK);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbLuongKhoan_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbLuongKhoan SelectByID(int MaLK)
        {
            ETbLuongKhoan OTbLuongKhoan = new ETbLuongKhoan();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLK",MaLK);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbLuongKhoan_SelectByID", pr);
            if (idr.Read())
              OTbLuongKhoan = GetOneTbLuongKhoan(idr);
            idr.Close();
            idr.Dispose();
            return OTbLuongKhoan;
        }
//-----------------------------------------------------------//
        private static ETbLuongKhoan GetOneTbLuongKhoan(IDataReader idr)
       {
            ETbLuongKhoan OTbLuongKhoan = new ETbLuongKhoan();
                if (idr["MaLK"] != DBNull.Value)
                    OTbLuongKhoan.MaLK = (int)idr["MaLK"];
                if (idr["TenLK"] != DBNull.Value)
                    OTbLuongKhoan.TenLK = (string)idr["TenLK"];
                if (idr["MoTa"] != DBNull.Value)
                    OTbLuongKhoan.MoTa = (string)idr["MoTa"];
            return OTbLuongKhoan;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaLK)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaLK",MaLK);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLuongKhoan_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbLuongKhoan> ListAll()
        {
            List<ETbLuongKhoan> list = new List<ETbLuongKhoan>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbLuongKhoan_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbLuongKhoan(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}
